package com.toune.villager.activity

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.qmuiteam.qmui.util.QMUIDeviceHelper
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.toune.dltools.ui.DLBaseActivity
import com.toune.villager.R
import com.toune.villager.adapter.GoodsListAdapter
import com.toune.villager.adapter.GoodsSubTypeAdapter
import com.toune.villager.adapter.GoodsTypeAdapter
import com.toune.villager.bean.GoodsListBean
import com.toune.villager.bean.GoodsTypeBean
import com.toune.villager.model.GoodsListModel
import com.toune.villager.model.LifeModel
import kotlinx.android.synthetic.main.activity_goods_list.*
import kotlinx.android.synthetic.main.activity_goods_list.swipeRefreshLv
import kotlinx.android.synthetic.main.fragment_life.*
import kotlinx.android.synthetic.main.layout_goods_type.*
import kotlinx.android.synthetic.main.layout_goods_type.view.*

class GoodsListActivity : DLBaseActivity<GoodsListModel>() {
    override val layout: Int
        get() = R.layout.activity_goods_list
    override val titleStr: String?
        get() = "商品列表"


    var goodsTypeId = 0
    var goodsTypeName = ""
    lateinit var goodsTypeModel: LifeModel
    override fun init(savedInstanceState: Bundle?) {
        goodsTypeModel = addViewModel<LifeModel>()
        goodsTypeModel.getGoodsType()
        goodsTypeModel.goodsTypeBeanValue.observe(this, {
            goodsTypeDataBeans.clear()
            goodsTypeDataBeans.addAll(it)
        })


        goodsTypeId = intent.extras!!.getInt("goodsTypeId")
        goodsTypeName = intent.extras!!.getString("goodsTypeName")!!
        setTitleStr(goodsTypeName)
        mViewModel.getGoodsList(goodsTypeId, page)

        mViewModel.goodsList.observe(this, {
            if (page == 1) {
                dataListBeans.clear()
                swipeRefreshLv.isRefreshing = false
            } else {
                if (adapter != null) {
                    adapter!!.loadMoreModule.loadMoreEnd()
                }
            }
            dataListBeans.addAll(it)
            setAdapter()
        })
    }

    override fun initEventAndData() {
        listStyleTv.setOnClickListener {
            if (GoodsListBean.RowsBean.style == GoodsListBean.RowsBean.GRID_STYLE) {
                listStyleTv.isSelected = true
                rclViewCount = 1
                GoodsListBean.RowsBean.style = GoodsListBean.RowsBean.LIST_STYLE
            } else {
                listStyleTv.isSelected = false
                rclViewCount = 2
                GoodsListBean.RowsBean.style = GoodsListBean.RowsBean.GRID_STYLE
            }
            setAdapter()
        }

        swipeRefreshLv.setOnRefreshListener {
            page = 1
            mViewModel.getGoodsList(goodsTypeId, page)
        }

        selectTypeTv.setOnClickListener {
            showGoodsTypePopu()
        }
    }

    var adapter: GoodsListAdapter? = null
    val dataListBeans = ArrayList<GoodsListBean.RowsBean>()
    var rclViewCount = 2
    var page = 1
    lateinit var staggeredGridLayoutManager: StaggeredGridLayoutManager
    private fun setAdapter() {
        if (adapter == null) {
            adapter = GoodsListAdapter(dataListBeans)
            staggeredGridLayoutManager =
                StaggeredGridLayoutManager(rclViewCount, StaggeredGridLayoutManager.VERTICAL)
            goodsRclView.layoutManager = staggeredGridLayoutManager
            goodsRclView.adapter = adapter
            adapter!!.loadMoreModule.isAutoLoadMore = false
            adapter!!.loadMoreModule.setOnLoadMoreListener {
                page += 1
                mViewModel.getGoodsList(goodsTypeId, page)
            }
            adapter!!.setOnItemClickListener { adapter, view, position ->
                startToActivity(ShopDetailActivity::class.java)
            }
        } else {
            staggeredGridLayoutManager.spanCount = rclViewCount
            adapter!!.notifyDataSetChanged()
        }
    }

    var goodsTypePopu: QMUIPopup? = null
    lateinit var popuView: View
    fun showGoodsTypePopu() {
        if (goodsTypePopu == null) {
            popuView = View.inflate(context, R.layout.layout_goods_type, null)
            goodsTypePopu = QMUIPopups.popup(
                context,
                QMUIDisplayHelper.getScreenWidth(context),
                QMUIDisplayHelper.dpToPx(400)
            )
                .preferredDirection(QMUIPopup.DIRECTION_BOTTOM)
                .view(popuView)
                .arrow(true)
                .shadow(true)
                .animStyle(QMUIPopup.ANIM_GROW_FROM_LEFT)
                .dimAmount(0.6f)
                .show(headerRv)
            setGoodsTypeAdapter()
            if (goodsTypeDataBeans.size > 0) {
                setGoodsSubTypeAdapter(goodsTypeDataBeans[selectedGoodsTypePos].dlGoodsSubTypeList)
            }
        } else {
            goodsTypePopu!!.show(headerRv)
        }
    }


    var goodsTypeAdapter: GoodsTypeAdapter? = null
    var goodsSubTypeAdapter: GoodsSubTypeAdapter? = null
    val goodsTypeDataBeans: MutableList<GoodsTypeBean.RowsBean> = ArrayList()
    val goodsSubTypeDataBeans: MutableList<GoodsTypeBean.RowsBean.DlGoodsSubTypeListBean> =
        ArrayList()

    var selectedGoodsTypePos = 0
    fun setGoodsTypeAdapter() {
        if (goodsTypeAdapter == null) {
            goodsTypeAdapter = GoodsTypeAdapter(R.layout.adapter_good_type, goodsTypeDataBeans)
            popuView.leftRclView.layoutManager = LinearLayoutManager(context)
            popuView.leftRclView.adapter = goodsTypeAdapter
            goodsTypeAdapter!!.setOnItemClickListener { adapter, view, position ->
                selectedGoodsTypePos = position
                goodsTypeAdapter!!.setSelectPos(position)
                setGoodsSubTypeAdapter(goodsTypeDataBeans[position].dlGoodsSubTypeList)
            }
        } else {
            goodsTypeAdapter!!.notifyDataSetChanged()
        }
    }


    fun setGoodsSubTypeAdapter(dataBeans: MutableList<GoodsTypeBean.RowsBean.DlGoodsSubTypeListBean>) {
        goodsSubTypeDataBeans.clear()
        goodsSubTypeDataBeans.addAll(dataBeans)
        if (goodsSubTypeAdapter == null) {
            goodsSubTypeAdapter =
                GoodsSubTypeAdapter(R.layout.adapter_good_sub_type, goodsSubTypeDataBeans)
            popuView.rightRclView.layoutManager = LinearLayoutManager(context)
            popuView.rightRclView.adapter = goodsSubTypeAdapter
            goodsSubTypeAdapter!!.setOnItemClickListener { adapter, view, position ->
                page = 1
                mViewModel.getGoodsList(goodsSubTypeDataBeans[position].id, page)
                setTitleStr(goodsSubTypeDataBeans[position].name)
                goodsTypePopu!!.dismiss()
            }
        } else {
            goodsSubTypeAdapter!!.notifyDataSetChanged()
        }
    }
}